#if !SERVER
using ActorSystem;
using Net.Client;
using Net.Common;
using Net.Component;
using Net.Share;
using UnityEngine;

namespace AOIExample
{
    public abstract partial class Actor : ActorBase
    {
        public bool IsLocal => Identity == ClientBase.Instance.UserId;
        public float moveSpeed = 6f;

        public override void OnOperation(in Operation opt)
        {
            switch (opt.cmd)
            {
                case Command.EnterArea:
                    EnterArea(opt);
                    break;
                case Command.ExitArea:
                    ExitArea(opt);
                    break;
                case Command.SyncPosition:
                    SyncPosition(opt);
                    break;
                default:
                    base.OnOperation(opt);
                    break;
            }
        }

        private void EnterArea(in Operation opt)
        {
            var viewPrefab = GetViewPrefab();
            View = Object.Instantiate(viewPrefab);
            View.Actor = this;
            View.transform.SetPositionAndRotation(opt.position, opt.rotation);
            View.gameObject.SetActive(true);
            if (opt.identity == ClientBase.Instance.UserId)
            {
                View.name = ClientBase.Instance.UserId.ToString();
                View.GetComponent<AOIObject>().IsLocal = true;
                Object.FindObjectOfType<SimpleCameraController>().target = View.transform;
            }
        }

        protected abstract ActorView GetViewPrefab();

        private void ExitArea(in Operation opt)
        {
            View.gameObject.SetActive(false);
        }

        private void SyncPosition(in Operation opt)
        {
            transform.SetPositionAndRotation(opt.position, opt.rotation);
        }

        public override void OnUpdate()
        {
            if (View == null)
                return;
            View.transform.position = Vector3.Lerp(View.transform.position, transform.Position, 0.25f);
            View.transform.rotation = Quaternion.Lerp(View.transform.rotation, transform.Rotation, 0.25f);
        }
    }
}
#endif